package com.rh.i_mes.controller.ums;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.i_mes.util.EasyPoiExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import com.rh.i_mes.model.UmsPermission;
import com.rh.i_mes.service.IUmsPermissionService;
import com.rh.i_mes.common.model.PageResult;
import com.rh.i_mes.common.model.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 权限表
 *
 * @author hqb
 * @date 2020-09-12 16:31:21
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "权限表")
@RequestMapping("/ums")
public class UmsPermissionController {
    @Autowired
    private IUmsPermissionService umsPermissionService;

    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/umsPermission/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<UmsPermission> list = umsPermissionService.findList(params);
        return Result.succeed(PageResult.restPage(list), "查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/umsPermission/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        UmsPermission model = umsPermissionService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping("/umsPermission/save")
    public Result save(@RequestBody UmsPermission umsPermission) {
        umsPermissionService.saveOrUpdate(umsPermission);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/umsPermission/del/{id}")
    public Result delete(@PathVariable Long id) {
        return umsPermissionService.delete(id);
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/umsPermission/leadIn")
    public Result leadIn(MultipartFile excel, String cuId) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<UmsPermission> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, UmsPermission.class);
            rowNum = list.size();
            if (rowNum > 0) {
                //无该用户信息
                list.forEach(u -> {
                    umsPermissionService.save(u);
                });
                return Result.succeed("成功导入信息" + rowNum + "行数据");
            }
        }
        return Result.failed("导入失败");
    }

    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/umsPermission/leadOut")
    public void leadOut(String cuId, HttpServletResponse response) throws IOException {
        List<UmsPermission> umsPermissions = new ArrayList<>();
        List<UmsPermission> umsPermissionList = umsPermissionService.list(new QueryWrapper<UmsPermission>().eq("cu_id", cuId));
        if (umsPermissionList.isEmpty()) {
            umsPermissions.add(umsPermissionService.getById(0));
        } else {
            for (UmsPermission umsPermission : umsPermissionList) {
                umsPermissions.add(umsPermission);
            }
        }
        //导出操作
        EasyPoiExcelUtil.exportExcel(umsPermissions, "权限表导出", "权限表导出", UmsPermission.class, "umsPermission.xls", response);

    }
    /**
     * 查询全部树状权限列表
     */
    @ApiOperation(value = "查询全部树状权限列表")
    @PostMapping("/umsPermission/allTree")
    public Result selectAllPermissionStructuring() {
        return Result.succeed(umsPermissionService.selectAllPermissionStructuring(0), "查询成功");
    }
}